package com.atguigu.juc.interview.queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author leimingchao
 * @create 2021-04-21 18:04
 */
public class BlockingQueueTest {
    public static void main(String[] args) {
        BlockingQueue<String> blockingQueue=new ArrayBlockingQueue<>(3);

        //System.out.println(blockingQueue.add("a"));
        //System.out.println(blockingQueue.add("b"));
        //System.out.println(blockingQueue.add("c"));
        //报错，队列已满
        //System.out.println(blockingQueue.add("X"));

        //System.out.println(blockingQueue.remove());
        //System.out.println(blockingQueue.remove());
        //System.out.println(blockingQueue.remove());
        //报错，队列为空
        //System.out.println(blockingQueue.remove());


        //System.out.println(blockingQueue.add("a"));
        //System.out.println(blockingQueue.add("b"));
        //移除队首元素，如果队列为空，抛出异常
        //System.out.println(blockingQueue.element());
        //移除队首元素，如果队列为空，返回null
        //System.out.println(blockingQueue.peek());

        //向队列中添加元素，如果队列已满，不会报错，返回一个false
        //System.out.println(blockingQueue.offer("a"));
        //System.out.println(blockingQueue.offer("b"));
        //System.out.println(blockingQueue.offer("c"));
        //System.out.println(blockingQueue.offer("X"));

        //移除队列中的元素，如果队列为空，不会报错，返回一个null
        //System.out.println(blockingQueue.poll());
        //System.out.println(blockingQueue.poll());
        //System.out.println(blockingQueue.poll());
        //System.out.println(blockingQueue.poll());


        //try {
            //blockingQueue.put("a");
            //blockingQueue.put("a");
            //blockingQueue.put("a");
            //阻塞
            ////blockingQueue.put("a");
            //
            //System.out.println(blockingQueue.take());
            //System.out.println(blockingQueue.take());
            //System.out.println(blockingQueue.take());
            //阻塞
            //System.out.println(blockingQueue.take());
        //} catch (InterruptedException e) {
        //    e.printStackTrace();
        //}
        try {
            System.out.println(blockingQueue.offer("a", 3L, TimeUnit.SECONDS));
            System.out.println(blockingQueue.offer("a", 3L, TimeUnit.SECONDS));
            System.out.println(blockingQueue.offer("a", 3L, TimeUnit.SECONDS));
            System.out.println(blockingQueue.offer("b", 3L, TimeUnit.SECONDS));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
